package com.dom4j;

import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ZF {
	//读写套餐.xml里面的信息
	static Document document = null;
	static Element root = null;
	//建立类属性，在别的类里直接调用套餐.xml里的套餐信息，做到一处更改，处处更改。
	static Element hlElement=null;
	static Element wcElement=null;
	static Element crElement=null;
	//创建对象
	static {
		SAXReader reader = new SAXReader();
		try {
			document = reader.read("套餐.xml");
			root = document.getRootElement();
			hlElement=(Element) root.elements().get(0);
			wcElement=(Element) root.elements().get(1);
			crElement=(Element) root.elements().get(2);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
//显示套餐名称
	public static void tcm() {
		Iterator it = root.elementIterator();
		int i = 0;
		while (it.hasNext()) {
			Element element = (Element) it.next();
			System.out.println(++i + "." + element.attributeValue("name"));
		}
	}
	//显示套餐具体信息
	public static void show(){
		Iterator it = root.elementIterator();
		int i = 0;
		while (it.hasNext()) {
			Element element = (Element) it.next();
			System.out.println(++i + "." + element.attributeValue("name"));
			Element cElement1=element.element("通话时间");
			Element cElement2=element.element("上网流量");
			Element cElement3=element.element("短信条数");
			Element cElement4=element.element("资费");
			if(!cElement1.getText().equals("")){
		           System.out.println("通话时间"+cElement1.getTextTrim()+"分钟");
			}if(!cElement2.getText().equals("")){
		           System.out.println("上网流量"+cElement2.getTextTrim()+"MB");
			}if(!cElement3.getText().equals("")){
		           System.out.println("短信条数"+cElement3.getTextTrim()+"条");
			}if(!cElement4.getText().equals("")){
		           System.out.println("资费"+cElement4.getTextTrim());
			}
			System.out.println("*************************");
		}
	}
}
